﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Cache;
using System.Text;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows;
using EzDesk.Utility.Extensions;

namespace EzDesk.Generic.WPF.Converter
{

    public class StringToImageSource : System.Windows.Data.IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {


            if(value == null) return null;
            //throw new NotImplementedException();
            if (value.GetType() != typeof(string))
                throw new Exception("Input object should be a string!");
            if (targetType != typeof(ImageSource))
                throw new Exception("Input object should be a string!");

            try
            {
                return BitmapFrame.Create(new Uri(value.ToString(), UriKind.RelativeOrAbsolute), new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore));
            }
            catch (Exception exp)
            {
                LogEx.EXCEPT("StringToImageSource", string.Format("创建图片资源失败 URL: {0}", value));
                return null;
                return BitmapFrame.Create(new Uri("/EzDesk.DesktopClient;component/Images/headshot_default.png", UriKind.RelativeOrAbsolute));
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class ImageCacherConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                var location = new Uri(value.ToString(), UriKind.RelativeOrAbsolute);
                var cachePolicy = new RequestCachePolicy(RequestCacheLevel.Default);
                return new BitmapImage(location, cachePolicy);
            }
            catch (Exception)
            {
                return null;
            }
            
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class TrueToCollapsedFalseToVisibility : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Visibility rv = Visibility.Collapsed;
            try
            {
                var x = bool.Parse(value.ToString());
                rv = x ? Visibility.Collapsed : Visibility.Visible;
            }
            catch (Exception)
            {
            }
            return rv;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }

    public class BooleanToHiddenVisibility : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Visibility rv = Visibility.Visible;
            try
            {
                var x = bool.Parse(value.ToString());
                rv = x ? Visibility.Visible : Visibility.Collapsed;
            }
            catch (Exception)
            {
            }
            return rv;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }

    }

    public class XamlStringToFrameworkElement : System.Windows.Data.IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null) return null;

            if (value.GetType() != typeof(string))
                throw new Exception("Input object should be a string!");

            try
            {
                LogEx.TRACE("ConvertXamlToUIElement", string.Format("XAML Source: {0}", value));
                return System.Windows.Markup.XamlReader.Parse(value.ToString());
            }
            catch (Exception exp)
            {
                LogEx.EXCEPT("ConvertXamlToUIElement", "XAML Parse 错误", exp);
                var element = new TextBlock { Text = "该信息解析出错" };
                return element;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class DateTimeToString : System.Windows.Data.IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null) return null;

            if (value.GetType() != typeof(DateTime))
                throw new Exception("Input object should be a DateTime!");

            try
            {
                var d = (DateTime)value;
                return d.ToNiceTime();
            }
            catch (Exception exp)
            {
                return "Convert Error!";
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }


}
